package org.example.bingevent.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.example.bingevent.common.Result;
import org.example.bingevent.mapper.UserMapper;
import org.example.bingevent.mapper.YanMapper;
import org.example.bingevent.pojo.Users;
import org.example.bingevent.pojo.Yan;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/yan")
@CrossOrigin
public class YanController {
    @Resource
    private YanMapper yanMapper;

    @PostMapping("/add") //用来登录
    public Result<?> reg(@RequestBody Yan yan){
        //selectone 只接收一个数据
        Yan yan1 = yanMapper.selectOne(new QueryWrapper<Yan>().eq("user_id", yan.getUserId()).eq("yan_year", yan.getYanYear()));
        yan.setStatus("否");
        if(yan1 != null){
            return Result.error("-1","当前年度你已经申请过了");
        }
        yanMapper.insert(yan);
        return Result.success();

    }

    @PostMapping("/edit") //用来登录
    public Result<?> edit(@RequestBody Yan yan){
        if(yan.getStatus().equals("拒绝")){
            yan.setStatus("否");
        }
        yanMapper.updateById(yan);
        return Result.success();

    }

    @DeleteMapping("/del/{id}") //用来登录
    public Result<?> edit(@PathVariable("id") Integer id){
        yanMapper.deleteById(id);
        return Result.success();

    }



    @GetMapping("/list") //用来登录
    public Result<?> list(Integer pageNum, Integer pageSize,Yan yan,Users users){
        QueryWrapper<Yan> yanQueryWrapper = new QueryWrapper<>();
        yanQueryWrapper.eq("yan_year", yan.getYanYear());
        Page<Yan> yanYear = yanMapper.selectPage(new Page<>(pageNum, pageSize),yanQueryWrapper);
        return Result.success(yanYear);

    }


}

